<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Script-Type" content="text/css" />
<title>修改配方</title>
<link type="text/css" href="/css/tablecloth.css" rel="stylesheet" media="screen" />
<script type="text/javascript" src="/js/jquery-1.4.4.js"></script>
<script type="text/javascript" src="/js/tablecloth.js" ></script>
<script type="text/javascript" src="/js/tableinput.js" ></script>
<script language="javascript">
	$(document).ready(function() {
		$("select#vectors").change(function() {
			$("h2#classfication").load("/vector/findClassificationByVectorId.do", {id:$(this).val()});
		});
		$("select#nutriments").change(function() {
			var nid = $(this).val();
			$.post("/nutriment/findFortifierByIdToJson.do", {id:nid}, function(data){
				$("#tableFortifier").find("tr:not(:first)").remove();
				var html;
				$.each(data.fortifiers, function(i, item) {
					html += "<tr nid='"+nid+"' fid='"+item.id+"'>";
					html += "<td>" + item.name + "</td>";
					html += "<td>" + item.purity + "</td>";
					html += "<td>" + item.loss + "</td>";
					html += "<td>" + item.cost + "</td>";
					html += "<td>" + item.ratio + "</td>";
					html += "<td>" + item.unit + "</td>";
					html += "<td><input type='button' value='添加' onclick='addFortifier($(this))'/></td>";
					html += "</tr>";
				});
				$("#tableFortifier").find("tr:first").after(html);
				tablecloth();
			}, "json");
			$.post("/nutriment/findByIdToJson.do", {id:nid}, function(data){
				$("#descriptionValue").html(""+data.description);
			});
			var cid = $("select#vectors").val();
			$.post("/nutriment/findStandardByClassificationToJson.do", {nid:nid, cid:cid}, function(data){
				if (data.id != undefined) {
					$("#nutrimentValue").html(data.min);
					$("#minValue").html(data.min);
					$("#maxValue").html(data.max);
				}
				else {
					$("#nutrimentValue").html("0.1");
					$("#minValue").html("-");
					$("#maxValue").html("-");
				}
			}, "json");
			var id = $("#nutriments").val();
			$.post("/nutriment/findByIdToJson.do", {id:id}, function(data){
				$("#unitValue").html(data.unit);
			}, "json");
		});
		$("select#vectors").change();
		$("select#nutriments").change();
		$("#nutrimentValue").blur(function(){
			var min = $("#minValue").html();
			var max = $("#maxValue").html();
			var val = $(this).html();
			min = min=="-"?"0":min;
			max = max=="-"?"0":max;
			if (eval(val) == 0 || eval(val) < eval(min) || (eval(max) != 0 && eval(val) > eval(max))) {
				if (val=="0")
					$(this).html("0.1");
				else
					$(this).html(min);
				alert("请输入标准范围内");
			}
		});
	});
	function addFortifier(obj) {
		var tr = $(obj).parent().parent();
		var fid = tr.attr("fid");
		var fname = tr.find("td:first").text();
		var nid = tr.attr("nid");
		var nname = $("select#nutriments").find("option:selected").text();
		var last = $("#tableNutrimentAndFortifier").find("tr:last");
		var html;
		html = "<tr nid='"+nid+"' fid='"+fid+"'>";
		html += "<td>"+nname+"</td>";
		html += "<td>"+fname+"</td>";
		// calculate fortifier quantity
		var nutriment = $("#nutrimentValue").html();
		var nunit = $("#unitValue").html();
		nutriment = changeToBaseUnit(nunit, nutriment);
		var purity = tr.find("td:eq(1)").html();
		var loss = tr.find("td:eq(2)").html();
		var cost = tr.find("td:eq(3)").html();
		var ratio = tr.find("td:eq(4)").html();
		var funit = tr.find("td:eq(5)").html();
		
		fortifier = nutriment / (ratio * purity * (1-loss));
		fortifier = changeFromBaseUnit(funit, fortifier);
		cost = cost * fortifier;
		
		html += "<td>"+fortifier.toFixed(2)+"</td>";
		html += "<td>"+funit+"</td>";
		html += "<td>"+cost.toFixed(2)+"</td>";
		html += "<td><input type='button' value='删除' onclick='removeFortifier($(this))'/></td>";
		last.after(html);
		tablecloth();
	}
	function removeFortifier(obj) {
		$(obj).parent().parent().remove();
	}
	function changeToBaseUnit(unit, value){
		if (unit == "ug")
			return value;
		else if (unit == "mg")
			return value * 1000;
		else if (unit == "g")
			return value * 1000000;
		return 0;
	}
	function changeFromBaseUnit(unit, value){
		if (unit == "ug")
			return value;
		else if (unit == "mg")
			return value / 1000;
		else if (unit == "g")
			return value / 1000000;
		return 0;
	}
	function calculatePrice(){
		var price = Number("0");
		$.each($("#tableNutrimentAndFortifier").find("tr:not(:first)"), function(index, value){
			price += Number($(value).children("td:eq(4)").html());
		});
		$("#totalPrice").html((price/10).toFixed(2)+"元/千克");
	}
	function addFormulation()
	{
		var name = $("#fName").val();
		var vid = $("select#vectors").val();
		var json = "{\"name\":\""+name+"\",\"vid\":"+vid+",\"fortifiers\":[";
		$.each($("#tableNutrimentAndFortifier").find("tr:not(:first)"), function(index, value){
			json += "{\"id\":"+$(value).attr("fid")+",\"mass\":"+$(value).children("td:eq(2)").html()+"},";
		});
		json += "]}";
		
		var form = document.getElementById("addFormualtionForm");
		form.json.value = json;
		form.submit();
		alert("添加成功");
	}
</script>
</head>

<body>
	<%@ taglib uri="/WEB-INF/tld/list-taglib.tld" prefix="tj" %>
	<form action = "/formulation/addFormulation.do" method="post" id="addFormualtionForm">
		<input type="hidden" name="json" />
	</form>	
			
	<h2>配方名：
	<input type="text" id="fName" value="请输入配方名" onmouseover="this.style.borderColor='#FF6600'" onmouseout="this.style.borderColor=''" onfocus="if (value =='请输入配方名'){value=''}" onblur="if (value ==''){value='请输入配方名'}"/>
	</h2>
	<p>载体：<tj:list id="vectors" list="${vectors}"></tj:list></p>
	<h2>分类：</h2><h2 id="classfication"></h2>
	
	<div class="tablecontent">
	<!-- 营养素/强化剂列表 -->
	<table id="tableNutrimentAndFortifier" cellspacing="0" cellpadding="0">
		<tr>
			<th class="title1">营养素</th>
			<th class="title2">强化剂</th>
			<th class="title3">用量</th>
			<th class="title4">单位</th>
			<th class="title5">价格</th>
			<th class="title6">删除</th>
		</tr>
	</table>
	<br/>
	<!-- 营养素选择 -->
	<table id="tableNutriment" cellspacing="0" cellpadding="0">
		<tr>
			<th class="title1">营养素</th>
			<th class="title2">单位</th>
			<th class="title3">用量</th>
			<th class="title4">描述</th>
			<th class="title5">最小值</th>
			<th class="title6">最大值</th>
		</tr>
		<tr>
			<td>
				<tj:list id="nutriments" list="${nutriments}"></tj:list>
			</td>
			<td id="unitValue">-</td>
			<% out.print("<td id='nutrimentValue' needInput='true'>0</td>"); %>
			<td id="descriptionValue">null</td>
			<td id="minValue">0</td>
			<td id="maxValue">0</td>
		</tr>
	</table>
	<br/>
	<!-- 强化剂 -->
	<table id="tableFortifier" cellspacing="0" cellpadding="0">
		<tr>
			<th class="title1">强化剂</th>
			<th class="title2">纯度</th>
			<th class="title3">损失</th>
			<th class="title4">成本</th>
			<th class="title5">比例</th>
			<th class="title6">单位 </th>
			<th class="title7">添加</th>
		</tr>
	</table>
	<input type="button" onclick="calculatePrice()" value="计算" />
	<input type="button" onclick="addFormulation()" value="创建配方" />
	</div>
		
    <br /><hr />
	<h4>符合国家标准</h4>
	<h4>此配方单价为：</h4>
	<h4 id="totalPrice"></h4>
</body>
</html>
